// 法一：哈希
const fn = (arr, key) => {
  const mapping = new Map();
  let len = arr.length;
  for (let i = 0; i < len; i++) {
    if (mapping.has(key - arr[i])) {
      let index = mapping.get(key - arr[i]);
      return [index, i];
    } else {
      mapping.set(arr[i], i);
    }
  }
};

// 暴力
const fn2 = (arr, key) => {
  let len = arr.length;
  for (let i = 0; i < len; i++) {
    let cur = arr[i];
    for (let j = i; i < len; j++) {
      if (arr[j] + cur === key) {
        return [i, j];
      }
    }
  }
};

console.log(fn2([2, 11, 7, 15], 9));
